﻿using System;
using System.Collections.Generic;
using CivilRegistry.ControlledModel.Extensions;

namespace CivilRegistry.ControlledModel
{
    /// <summary>
    /// The hospital object.
    /// </summary>
    public partial class Hospital: User
    {
        #region Methods

        #region Finder Methods

        /// <summary>
        /// Finds the <see cref="Hospital"/> by Id.
        /// </summary>
        /// <param name="id">The id of the <see cref="Hospital"/>.</param>
        /// <returns>Found <see cref="Hospital"/> object, null if not found.</returns>
        public static Hospital FindById(int id)
        {
            return (Hospital)repository.FindBy(user => user.UserId == id && user is Hospital);
        }

        /// <summary>
        /// Retrives the deactive <see cref="Hospital"/>s.
        /// </summary>
        /// <returns>The deactive <see cref="Hospital"/>s.</returns>
        public static List<Hospital> GetDeactiveRecords()
        {
            List<Hospital> hospitalList = new List<Hospital>();
            repository.FindAll(hospital => hospital.DeactivateReason != null).ForEach(hospital => hospitalList.Add((Hospital)hospital));
            return hospitalList;
        }

        /// <summary>
        /// Retrives the <see cref="BirthReport"/>s.
        /// </summary>
        /// <returns>The <see cref="BirthReport"/>s.</returns>
        public List<BirthReport> GetBirthReports()
        {
            return BirthReport.FindAll(birthReport => birthReport.CivilHospital == this);
        }

        #endregion

        #region CUD Methods

        /// <summary>
        /// To add the <see cref="BirthReport"/>.
        /// </summary>
        /// <param name="birthReport">The <see cref="BirthReport"/> to add.</param>
        /// <returns>Added <see cref="BirthReport"/>.</returns>
        public BirthReport AddBirthReport(BirthReport birthReport)
        {
            birthReport.Required("birthReport");
            birthReport.CivilHospital = this;
            birthReport.AddedUser = this;
            birthReport.AddedDate = DateTime.Now;
            return BirthReport.Insert(birthReport);
        }

        /// <summary>
        /// To update the <see cref="BirthReport"/>.
        /// </summary>
        /// <param name="birthReport">The <see cref="BirthReport"/> to update.</param>
        /// <returns>The updated <see cref="BirthReport"/>.</returns>
        public BirthReport UpdateBirthReport(BirthReport birthReport)
        {
            birthReport.ModifiedDate = DateTime.Now;
            birthReport.ModifiedUser = this;
            return birthReport.SaveChanges();
        }

        #endregion

        #endregion
    }
}
